package com.qiwenshare.file.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qiwenshare.common.anno.MyLog;
import com.qiwenshare.common.result.RestResult;
import com.qiwenshare.file.api.RoleService;
import com.qiwenshare.file.component.InitData;
import com.qiwenshare.file.domain.SysRole;
import com.qiwenshare.file.dto.role.AddRoleDto;
import com.qiwenshare.file.dto.role.DeleteRoleDto;
import com.qiwenshare.file.dto.role.ModifyRoleDto;
import com.qiwenshare.file.util.SpringBeanUtil;
import com.qiwenshare.file.vo.SysRoleVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 角色表(Role)表控制层
 *
 * @author makejava
 * @since 2023-09-16 19:52:32
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    /**
     * 服务对象
     */
    @Resource
    private RoleService roleService;

    public static final String CURRENT_MODULE = "角色管理";

    @Resource
    private InitData initData;


    /**
     * 得到所有的角色
     *
     * @return
     */
    @Operation(summary = "得到所有的角色列表", description = "得到所有的角色列表", tags = {"角色管理"})
    @RequestMapping(value = "/allList", method = RequestMethod.GET)
    @ResponseBody
    public RestResult<SysRole> queryAllRoleList() {
        return RestResult.success().data(roleService.queryAllRoleList());
    }

    /**
     * 获取用户的角色列表
     *
     * @return
     */
    @Operation(summary = "获取用户的角色列表", description = "获取用户的角色列表", tags = {"角色管理"})
    @RequestMapping(value = "/listByUser", method = RequestMethod.GET)
    @ResponseBody
    public RestResult<Long> listByUser(@RequestParam(value = "userId") String userId) {
        return RestResult.success().data(roleService.queryListByUser(userId));
    }


    /**
     * 得到所有的角色
     *
     * @return
     */
    @Operation(summary = "得到所有的角色列表", description = "得到所有的角色列表", tags = {"角色管理"})
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public RestResult<SysRoleVo> queryRoleList(@RequestParam(value = "roleName", required = false) String roleName,
                                               @Parameter(description = "当前页", required = true) long currentPage,
                                               @Parameter(description = "页面数量", required = true) long pageCount) {
        IPage<SysRoleVo> list = roleService.getRoleList(roleName, currentPage, pageCount);
        return RestResult.success().dataList(list.getRecords(), list.getTotal());
    }

    @Operation(summary = "新增角色", description = "新增角色", tags = {"角色管理"})
    @PostMapping(value = "/add")
    @MyLog(operation = "新增角色", module = CURRENT_MODULE)
    @ResponseBody
    public RestResult<String> add(@Valid @RequestBody AddRoleDto role) {
        RestResult result = roleService.insert(SpringBeanUtil.copyProperties(role, SysRole.class), role.getMenuIds());
        initData.manualInitData();
        return result;
    }


    @Operation(summary = "编辑角色", description = "编辑角色", tags = {"角色管理"})
    @PostMapping(value = "/modify")
    @MyLog(operation = "编辑角色", module = CURRENT_MODULE)
    @ResponseBody
    public RestResult<String> modify(@Valid @RequestBody ModifyRoleDto role) {
        RestResult result = roleService.update(SpringBeanUtil.copyProperties(role, SysRole.class), role.getMenuIds());
        initData.manualInitData();
        return result;
    }


    @Operation(summary = "删除角色", description = "删除角色", tags = {"角色管理"})
    @PostMapping(value = "/delete")
    @MyLog(operation = "删除角色", module = CURRENT_MODULE)
    @ResponseBody
    public RestResult<String> delete(@Valid @RequestBody DeleteRoleDto role) {
        RestResult result = roleService.deleteById(role.getRoleId());
        initData.manualInitData();
        return result;
    }

}

